const app = getApp()
import trigger from '../../utils/util.js'
Page({
    data: {
        gao_on: '',
        isOver: false,
        scroll_true: '',
        isShow: ''
    },
// 	guide_off:function(){
// 		this.setData({
// 			guide_on:''
// 		})
// 	},
    onLoad: function (options) {
        (trigger.trigger)(this)
        console.log(options);
        var that = this;
        this.page_num = 1
		
		//第一次进来
// 		if(options.new_url){
// 			that.setData({
// 				guide_on:'on',
// 			})
// 		}
		
		
		//授权
		app.checkSession(this,'../activityList/promise','','','','',options.activity_id,'',true);
		
		
    	//公告
    	app.request(app.apiDomain + "index.php?c=mall&a=notice", {
    		type: 'activity'
    	}, function(res) {
    		// console.log(res)
    		that.setData({
    			gao: res.data.data
    		});
        });
        // 详情
        app.request(app.apiDomain + "index.php?c=activity&a=detail", {
            activity_id: options.activity_id,
            page: 1,
            size: 10
    	}, function(res) {
            console.log(res)
            // 如果list数量没有size大,没必要加载
            if(res.data.data.list.length < 10){
                that.scroll_suo = true
            }
            // 判断当前活动状态
            var activity = res.data.data.activity
            if(activity.status==0){
                // 未开始
                if(res.data.data.user.length==0){
                    // 未参加
                    that.join = true
                    var statusImg='//resource.160nurse.com/canvas/healthy/img/activity/promise_1.png';
                }else {
                    // 已参加
                    that.join = false
                    var statusImg='//resource.160nurse.com/canvas/healthy/img/activity/promise_2.png';
                }
            }else if(activity.status==1){
                // 进行中
                that.join = false
                var statusImg='//resource.160nurse.com/canvas/healthy/img/activity/promise_6.png';
            }else if(activity.status==2){
                // 结束
                that.join = false
                var statusImg='//resource.160nurse.com/canvas/healthy/img/activity/promise_3.png';
                // 判断用户是否有守约
                if(res.data.data.user.status=='1'){
                    // 成功
                    var promiseImg = '//resource.160nurse.com/canvas/healthy/img/activity/promise_4.png'
                }else if(res.data.data.user.status=='2'){
                    var promiseImg = '//resource.160nurse.com/canvas/healthy/img/activity/promise_5.png'
                }
                that.setData({
                    isOver: true,
                    promiseImg: promiseImg
                })
            }
            // var icon = res.data.data.activity.currency.substring(0,res.data.data.activity.currency.indexOf('.'))
            var icon = parseInt(res.data.data.activity.currency)
            that.setData({
                activity: activity,
                list: res.data.data.list,
                user: res.data.data.user,
                activity_id: options.activity_id,
                statusImg: statusImg,
                icon: that.countIcon(icon.toString()),
                bail_currency: parseInt(activity.bail_currency),
                count: activity.count
            })
        });
    },
    getUserInfo: function () {
      var that = this
      if (app.getUserInfo(that)) {
        that.setData({
          user_auth: false
        })
      }
    },
    joinactivity(){
        var that = this
        // 参加活动
        if(this.join){
            // 提示扣币
            wx.showModal({
                title: '提示',
                content: '本次活动需支付'+that.data.activity.bail_currency+'健康币',
                success(res) {
                    if (res.confirm) {
                        // 改币 改图  改状态
                        var icon = parseInt(that.data.activity.currency)+parseInt(that.data.activity.bail_currency)
                        app.request(app.apiDomain + "index.php?c=activity&a=apply", {
                            activity_id: that.data.activity_id
                        },res=>{
                            console.log(res);
                            if(res.data.status==1){
                                // 报名成功
                                that.join = false
                                that.setData({
                                    isShow: 'on',
                                    user: res.data.data,
                                    count: parseFloat(that.data.count) + 1,
                                    statusImg: '//resource.160nurse.com/canvas/healthy/img/activity/promise_2.png'
                                })
                                setTimeout(()=>{
                                    that.setData({
                                        icon: that.countIcon(icon.toString()),
                                        isShow: ''
                                    })
                                },800)
                            }else {
                                wx.showToast({
                                    title: res.data.info,
                                    icon: 'none',
                                    duration: 2000
                                })
                            }
                        })
                    } else if (res.cancel) {
                      console.log('用户点击取消')
                    }
                }
            })
        }
    },
    // 下拉加载
    scroll_get(){
        if(this.scroll_suo) {return false}
        var that = this;
        this.setData({
            scroll_true: 'on'
        })
        this.page_num++
        app.request(app.apiDomain+'index.php?c=activity&a=detail',{
            activity_id: that.data.activity_id,
            page: that.page_num,
            size: 10
        },res=>{
            this.setData({
                scroll_true: ''
            })
            // console.log(res);
            if(res.data.data.list.length==0){
                wx.showToast({
                    title: '已经到底了',
                    icon: 'none',
                    duration: 2000
                })
                that.scroll_suo = true;
            }else {
                that.setData({
                    list: that.data.list.concat(res.data.data.list)
                })
            }
        })
    },
    // 计算数字
    countIcon(num){
        var num1 = num.substring(num.length-1,num.length);
        var num2 = num.substring(num.length-2,num.length-1)?num.substring(num.length-2,num.length-1):0;
        var num3 = num.substring(num.length-3,num.length-2)?num.substring(num.length-3,num.length-2):0;
        var num4 = num.substring(num.length-4,num.length-3)?num.substring(num.length-4,num.length-3):0;
        return [num1,num2,num3,num4]
    },
    gao_click: function() {
        this.setData({
            gao_on: 'on'
        })
    },
    gao_off: function() {
        this.setData({
            gao_on: ''
        })
    },
    onShareAppMessage: function () {
        console.log('分享')
		return {
            title: '早起守约,和我一起瓜分健康币吧~',
            path: '/pages/activityList/promise?activity_id=' + this.data.activity_id,
            imageUrl: 'https://resource.160nurse.com/canvas/healthy/img/activity/promise_8.png'
		}
    }
})